package com.jy.project.service.imp;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.jy.project.domain.Apply;
import com.jy.project.dto.ApplyDeptAdminQueryDTO;
import com.jy.project.domain.Material;
import com.jy.project.dto.*;
import com.jy.project.enums.ApplyTypeEnum;
import com.jy.project.enums.ApplyTypeMatericalEnum;
import com.jy.project.ex.BizException;
import com.jy.project.mapper.ApplyMapper;
import com.jy.project.mapper.MaterialMapper;
import com.jy.project.service.ApplyService;
import com.jy.project.service.imp.defult.DefaultApplyBusinessImp;
import com.jy.project.util.CodeUtils;
import com.jy.project.util.ServiceResultUtils;
import com.jy.project.vo.ApplyDeptAdminVO;
import com.jy.project.vo.ApplyQualitVO;
import com.jy.project.vo.ApplyUserVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service("applyQualityServiceImpl")
public class ApplyQualityServiceImpl extends DefaultApplyBusinessImp implements ApplyService {

    @Autowired
    private ApplyMapper applyMapper;

    @Autowired
    private MaterialMapper materialMapper;


    @Override
    public boolean insertApply(ApplyDTO dto) {

        ApplyQualityDTO applyQualityDTO=(ApplyQualityDTO) dto;
        String applyQualityStatus = applyQualityDTO.getApplyQualityStatus();
        if (StringUtils.isNotEmpty(applyQualityStatus)){
            String[] split = applyQualityStatus.split(",");
            if (split.length>0) {
                Set<Integer> set = new HashSet<>();
                for (String s : split) {
                    set.add(Integer.valueOf(s));
                }
                String str = JSONObject.toJSONString(set);
                dto.setExt(str);
            }
        }
        dto.setApplyType(ApplyTypeEnum.QUALITY.getCode());
        boolean bl = this.insertApplyBusiness(dto);
        if(bl==false){
            throw  new BizException("增加资产质量数据失败");
        }
        Example example=new Example(Material.class);
        example.createCriteria()
                .andEqualTo("materialNo",dto.getMaterialNo())
                .andEqualTo("isDelete",0);
        Material material=new Material();
        material.setQualityStatus(dto.getExt());
        bl=materialMapper.updateByExampleSelective(material,example)>0;
        if (bl==false){
            throw  new BizException("修改资产数据状态失败");
        }
        return true;
    }

    @Override
    public List<ApplyQualitVO> listApplyQualityVO(ApplyQueryDTO dto) {
        PageHelper.startPage(dto);
        return applyMapper.listApplyyQualityVO(dto);
    }

    @Override
    public Apply create() {
        Apply apply = super.create();
        apply.setApplyNo(CodeUtils.generateApplyNo(ApplyTypeMatericalEnum.QUALITY.getValue()));
        return apply;
    }

    @Override
    public List<ApplyDeptAdminVO> listDeptAdminApplyVO(ApplyDeptAdminQueryDTO dto) {
        return null;
    }

    @Override
    public boolean applyAdopt(ApplyAdoptDTO dto) {
        return true;
    }

    @Override
    public boolean applyReject(ApplyAdoptDTO dto) {
        return true;
    }

    @Override
    public ServiceResultUtils listMaterialDetailsVO(MaterialDetailsDTO dto) {
        return null;
    }

    @Override
    public List<ApplyUserVO> listUserApplyVO(ApplyUserQueryDTO dto) {
        return null;
    }

}
